package com.hwadee.caixiang.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.hwadee.caixiang.entity.Product;
import com.hwadee.caixiang.service.ProductService;

@Controller
public class ProductController {

	@Autowired
	private ProductService productService;

	@RequestMapping(value = "/product", method = RequestMethod.GET)
	public String list(Model model) {

		List<Product> products = productService.products();
		model.addAttribute("products", products);

		return "product";
	}
	
	@GetMapping("/product/{pid}")
	public String get(@PathVariable("pid") int pid, Model model) {
		Product product = productService.productfindByPid(pid);
		model.addAttribute("product", product);
		return "desc";
	}
	
	@RequestMapping(value = "/search", method = RequestMethod.POST)
	public String search(String searchname,Model model) {

		List<Product> productsOfSearch = productService.productsOfSearch(searchname);
		model.addAttribute("productsOfSearch",productsOfSearch);
		return "search";
	}
}
